package com.stenio.fancat.service.behavior;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stenio.fancat.domain.baby.BabyRelationship;
import com.stenio.fancat.domain.behavior.Behavior;
import com.stenio.fancat.domain.behavior.BehaviorLog;
import com.stenio.fancat.domain.bo.BehaviorBO;
import com.stenio.fancat.form.baby.BabyAccountForm;
import com.stenio.fancat.mapper.Behavior.BehaviorLogMapper;
import com.stenio.fancat.mapper.Behavior.BehaviorMapper;
import com.stenio.fancat.mapper.Behavior.BehaviorOperationImageMapper;
import com.stenio.fancat.mapper.baby.BabyRelationshipMapper;
import com.stenio.fancat.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class BehaviorTemplateService extends ServiceImpl<BehaviorMapper, Behavior> {

    @Autowired
    private BehaviorMapper behaviorMapper;

    @Autowired
    private BehaviorLogMapper behaviorLogMapper;

    @Autowired
    private BehaviorOperationImageMapper BehaviorOperationImageMapper;

    @Autowired
    private BabyRelationshipMapper babyRelationshipMapper;

    /**
     * 查询全部任务（首页）  首页展示的图片为用户上传最近的任务照片，没有的话不进行展示
     * @return
     */
//    public List<BehaviorBO> selectBehaviorList(BabyAccountForm babyAccountForm) {
//
//        List<BehaviorBO> list = new ArrayList<>();
//        List<Behavior> listBehavior = behaviorMapper.selectList(null);
//
//        listBehavior.stream().forEach(bo -> {
//            BehaviorBO behaviorBO = new BehaviorBO();
//            //任务模板信息
//            behaviorBO.setBehavior(bo);
//            //根据babyId查询他所关联的userId
//            List<BabyRelationship> listbaby = babyRelationshipMapper.selectUserListByBabyId(babyAccountForm.getBabyId());
//
//            //存放每个用户所发布的任务（列表）
//            List<BehaviorLog> loglist = new ArrayList<>();
//
//            listbaby.stream().forEach(uid ->{
//                //把每个任务列表拆开，然后add到loglist列表里面
//                //List<BehaviorLog> behaviorLogList = behaviorLogMapper.selectBehaviorLogListbyBid(bo.getBehaviorId(),uid.getUserId());
//                //behaviorLogList.stream().forEach(bv ->{
//                    loglist.add(bv);
//                });
//            });
//
//            //把取出的所有的活动取出它们的修改时间，转换后存放到数组里面
//            Long[] arr = new Long[loglist.size()];
//            for(int i = 0;i<loglist.size();i++){
//                arr[i] = Long.parseLong(DateUtil.format(loglist.get(i).getUpdateTime(),"yyyyMMddHHmmss"));
//            }
//            //进行排序
//            Arrays.sort(arr);
//            //取出最后一个就是最近发布的那个动态(修改的时间)
//            BehaviorLog behaviorLog = null;
//            for(int i = 0;i<loglist.size();i++){
//                Long time = Long.parseLong(DateUtil.format(loglist.get(i).getUpdateTime(),"yyyyMMddHHmmss"));
//                if(arr[arr.length-1]== time){
//                    behaviorLog = loglist.get(i);
//                    break;
//                }
//            }
//            //查询该任务的任务照片
//            behaviorBO.setBehaviorOperationImage(BehaviorOperationImageMapper.selectBehaviorOperationImages(behaviorLog.getOperationId()));
//
//            list.add(behaviorBO);
//        });
//
//        return list;
//    }



}
